package dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import model.AvailableRoom;
import model.RoomType;
import util.DBController;



public class AvailableRoomDaoImpl {

	public List<AvailableRoom> getAvailableRoomList(String start, String end, String rType , 
													String adult , String children , String noOfRooms , String night ) {
		
		
		// set initial value
		List<AvailableRoom> AvailableRoomList = new ArrayList<AvailableRoom>();
		
		AvailableRoom availroom = new AvailableRoom();
		availroom.setAdult(adult);
		availroom.setArrival(start);
		availroom.setChildren(children);
		availroom.setDeparture(end);
		availroom.setNights(night);	
		availroom.setUnit(noOfRooms);
		availroom.setRoomType(rType);
		RoomType  rt = new RoomType();
		RoomTypeDaoImpl roomTypeDaoObj = new dao.impl.RoomTypeDaoImpl();
		if (!rType.equals("All")) {					
			rt = roomTypeDaoObj.getRoomTypeById(rType);
			availroom.setRoomName(rt.getName());
		}
		else	availroom.setRoomName("No Preference");
		availroom.setUnit(noOfRooms);		
		AvailableRoomList.add(availroom);
		
		String condition = "";
		String condition1 ="" ;
		String condition2 = "";
		String condition3 = "";
		
		
		condition1 = "RoomID not in (Select RoomID from Booking, Room_has_Booking " +
							"where DATEDIFF(Start,'"+start+"') <= 0 " +
							"and DATEDIFF(end, '"+end+"') >= 0 " +
							"and Booking.ID = Room_has_Booking.BookingID " +
							"and Booking.bookingStatus='R' )" ;
		
		condition2 = " AND ((" + adult + "+" + children + ") <= ((RoomType.maxAdult + RoomType.maxChildren)) * " + noOfRooms + ") " +
								" AND (DateDiff('"+end+"' , '"+start+"') <= maxNight OR maxNight = NULL )AND DateDiff('"+end+"' , '"+start+"') >= minNight " ;
		
		if (!rType.equals("All"))
			condition3 = " and Room.TypeID='"+rType+"' " ;	
		
		condition = condition1 + condition2 + condition3;	
		
		ResultSet rs = DBController.retrieveRatesCount("Room", null, condition , "Room.TypeID");
		
		try {
			if( rs != null) {
				//rs.last();
				//if (rs.getRow() >= Integer.parseInt(noOfRooms)){					
					//rs.beforeFirst();
					while( rs.next() ) {
						if (Integer.parseInt(rs.getString("roomCount")) >= Integer.parseInt(noOfRooms)){						
							condition3 = " and Room.TypeID='"+rs.getString("TypeID")+"' " ;
							condition = "";
							condition = condition1 + condition2 + condition3;
							
							ResultSet rs2 = DBController.retrieveRates("Room", null, condition , "Room.TypeID");
							
							if (rs2 != null){
								double rates;
								while( rs2.next() ) {
									availroom = new AvailableRoom();
									availroom.setRoomType(rs.getString("TypeID"));
									availroom.setAdult(adult);
									availroom.setArrival(start);
									availroom.setChildren(children);
									availroom.setDeparture(end);
									availroom.setId(rs2.getString("RoomID"));
									availroom.setNights(night);
									availroom.setRates(rs2.getString("RoomID"));
									availroom.setRoomDescription(rs2.getString("Description"));
									availroom.setRoomName(rs2.getString("Name"));							
									availroom.setUnit(noOfRooms);
									rates = Double.parseDouble(rs2.getString("DiscountPercent"));
									rates = Double.parseDouble(rs2.getString("Rate")) - (( Double.parseDouble(rs2.getString("Rate"))/ 100 ) * Integer.parseInt(rs2.getString("DiscountPercent")));
									//availroom.setRoomType(rs.getString("Room.TypeID"));
									availroom.setRates(Double.toString(rates));
									AvailableRoomList.add(availroom);
								}								
								
							}						
						}					
						
				}
				
			}
			DBController.disconnect();
		} catch (Exception e) {
			System.out.println("Error at method getRoomList() of RoomDaoImpl.");
			e.printStackTrace();
		}
		
		return AvailableRoomList;
		
	}
	
	
}
